﻿using ChargingSystem4.Common.MyCache;
using Edge.Repository.Model;
using EMSClient.CForm;
using EMSClient.Config;
using Oliving.Common;
using Oliving.Common.LogHelper;
using Oliving.Common.MyHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChargingSystem4.CsForm
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        // EmsConfigHelper 

        private void FormLogin_Load(object sender, EventArgs e)
        {
            label2.Text = EmsConfigHelper.SoftName;
            this.TopMost = true;


            Splasher.Close();


            if (AppConfigHelper.isTest)
            {
                this.tb_Num.Text = "admin";
                this.tb_Pwd.Text = "123456";
            }
        }



        private void btn_Submit_Click(object sender, EventArgs e)
        {

            string code = this.tb_Num.Text;
            string pwd = this.tb_Pwd.Text;


            if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("账号或密码不能为空！"); return;
            }

            if (code.ToLower() != EmsConfigHelper.LoginName)
            {

                UserService dal = new UserService();
                var modelUser = dal.Query(F => F.Code == code && F.PassWord == pwd).FirstOrDefault();

                if (modelUser != null && modelUser.Id > 0)
                {
                    CacheClientHelper.UserDto = modelUser;

                    #region 清理本地空间

                    try
                    {
                        string myPath = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "log");
                        DelLog.Del(myPath);
                    }
                    catch { }

                    try
                    {
                        string tempPath = Path.Combine(System.Environment.CurrentDirectory, "temp");
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        };
                        DelLog.Del_jpg(tempPath);
                    }
                    catch { }

                    #endregion

                    this.DialogResult = DialogResult.OK;

                }
                else
                {
                    MessageBox.Show("账号或密码错误！"); return;
                }
            }
            else
            {
                if (EmsConfigHelper.LoginPwd == tb_Pwd.Text.Trim())
                {
                    //1	user3	123456	2	user3	user3	user3	user3	0
                    CacheClientHelper.UserDto = new CS_User()
                    {
                        Name = "admin",
                        Admin = "admin",
                        UserType = "2",
                        status = 0,
                        ClassName = "admin",
                        Code = "admin",
                        PassWord = "123456",
                        UserRemark = "admin",
                    };

                    #region 清理本地空间

                    try
                    {
                        string myPath = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "log");
                        DelLog.Del(myPath);
                    }
                    catch { }

                    try
                    {
                        string tempPath = Path.Combine(System.Environment.CurrentDirectory, "temp");
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        };
                        DelLog.Del_jpg(tempPath);
                    }
                    catch { }

                    #endregion

                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("密码错误");
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }


        #region 窗体移动
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

        }


        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();





        #endregion
         
        private void panel1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
